package org.eyeTopLight.config;

import org.eyeTopLight.po.userEntity.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义在jwtToken中放入的字段
 */
@Configuration
public class TokenJwtEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        // info是要往JWT中放入的信息
        Authentication userAuthentication = authentication.getUserAuthentication();//登录用户信息
        Integer user_id = ((User)userAuthentication.getPrincipal()).getUser_id();
        Map<String, Object> info = new HashMap<>();
        info.put("user_id",user_id);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);//设置附加信息
        return accessToken;
    }
}
